package com.atu.poker.dao.impl;

import java.util.List;

import com.atu.poker.dao.DAO;
import com.atu.poker.dao.UserInfoDAO;
import com.atu.poker.po.UserInfo;
import com.mysql.jdbc.Blob;

public class UserInfoDAOJdbcImpl extends DAO<UserInfo> implements UserInfoDAO {

	/**
	 * 添加用户用户信息
	 */
	@Override
	public void addUserInfo(UserInfo userinfo){
		String sql = "INSERT INTO t_user(user_id,city_id,face_img,sex,user_name,user_pwd,vip,room_key,user_chip,email,create_time,user_status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		update(sql, userinfo.getUserId(), userinfo.getCityId(), userinfo.getFaceImg(), userinfo.getSex(),
				userinfo.getUserName(), userinfo.getUserPwd(), userinfo.getVip(),userinfo.getRoomKey(), userinfo.getUserChip(),
				userinfo.getEmail(), userinfo.getCreateTime(), userinfo.getUserStatus());
	}

	/**
	 * 根据userId和userPwd获取用户信息，起别名和javabean对应
	 */
	@Override
	public UserInfo getUserInfo(String userId) {
		String sql = "SELECT user_id userId,city_id cityId,face_img faceImg,sex sex,user_name userName,vip vip,room_key roomKey,user_chip userChip,phone phone,email email,create_time createTime,user_status userStatus FROM t_user WHERE user_id = ?";
		UserInfo userInfo = get(sql, userId);
		return userInfo;
	}

	/**
	 * 修改用户密码，根据userId修改用户密码
	 */
	@Override
	public void upUserPwd(String email,String userPwd) {
		String sql = "UPDATE t_user SET user_pwd = ? WHERE email = ?";
		update(sql, userPwd, email);
	}

	/**
	 * 获取所有数据库用户
	 */
	@Override
	public List<UserInfo> getUserInfoList(){
		String sql = "SELECT user_id userId,city_id cityId,face_img faceImg,sex sex,user_name userName,user_pwd userPwd,vip vip,room_key roomKey,user_chip userChip,phone phone,email email,create_time createTime,user_status userStatus FROM t_user";
		return getForList(sql);
	}

	/**
	 * 根据userName获取用户信息的userId
	 */
	@Override
	public String getIdByEmail(String email) {
		String sql = "SELECT user_id userId FROM t_user WHERE email = ?";
		return getForValue(sql, email);
	}

	@Override
	public String getUserPwd(String email){
		String sql = "SELECT user_pwd userPwd FROM t_user WHERE email = ?";
		return getForValue(sql, email);
	}

	@Override
	public Blob getFaceImg(String userName) {
		String sql = "SELECT face_img faceImg FROM t_user WHERE user_name = ?";
		return getForValue(sql, userName);
	}

	@Override
	public String getIdByUserName(String userName) {
		String sql = "SELECT user_id userId FROM t_user WHERE user_name = ?";
		return getForValue(sql, userName);
	}

	@Override
	public void changeUserStatus(String userId,String userStatus) {
		String sql = "UPDATE t_user SET user_status = ? WHERE user_id = ?";
		update(sql, userStatus, userId);
	}

	@Override
	public String getUserStatus(String userId) {
		String sql = "SELECT user_status userStatus FROM t_user WHERE user_id = ?";
		return getForValue(sql, userId);
	}

	@Override
	public String getUserName(String userId) {
		String sql ="SELECT user_name userName FROM t_user WHERE user_id = ?";
		return getForValue(sql, userId);
	}
}
